//
//  WebViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/8/14.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "WebViewController.h"
//#import <WebViewJavascriptBridge.h>
#import "OCNative.h"
@interface WebViewController ()
{

    
}
@property (nonatomic,strong)  UIProgressView *progress;
@property (nonatomic,strong)  UIWebView *webView;


@end

@implementation WebViewController
{
    NJKWebViewProgress *_progressProxy;
    OCNative *ocNative;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self baseCtlSetTitle:@"网页"];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    
    ocNative = [[OCNative alloc]init];
    ocNative.webview = _webView;
    [self baseCtlSetLeftBtnTitle:nil andWithRightTitle:nil];
    
    [self.leftNavgationItem setAction:@selector(clickBack)];
    
    if (_urlStr) {
        NSURL *url =[[NSURL alloc] initWithString:_urlStr];
        
        NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];
        
        [_webView loadRequest:request];
    }
    if (_htmlStr) {
        NSURL* url = [NSURL fileURLWithPath:_htmlStr];
        NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
        [_webView loadRequest:request];
    }
    
    
}
- (void)createUI
{
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    [self.view addSubview:_webView];
    
    _webView.backgroundColor = [UIColor clearColor];
    _webView.scalesPageToFit =YES;
    _webView.delegate = _progressProxy;
    

    
    _progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progress.frame = CGRectMake(0, 0, ScreenWidth, 2);
    _progress.progressTintColor = [UIColor redColor];
    _progress.trackTintColor = [UIColor clearColor];
    
    [self.view addSubview:_progress];
}

- (void)clickBack
{
    if ([_webView canGoBack]) {
        [_webView goBack];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}

#pragma mark - delegate
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    DLog(@"progress%f",progress);
    if (progress == 0.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        _progress.progress = 0;
        [UIView animateWithDuration:0.27 animations:^{
            _progress.alpha = 1.0;
        }];
    }
    if (progress == 1.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [UIView animateWithDuration:0.27 delay:progress - _progress.progress options:0 animations:^{
            _progress.alpha = 0.0;
        } completion:nil];
    }
    [_progress setProgress:progress animated:NO];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSURL *url = [request URL];
    
    NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    if([[url scheme] isEqualToString:@"xljsbridge"]) {
        NSRange range = [requestString rangeOfString:@"://"];
        if (range.location == NSNotFound) {
            DLog(@"string was not found");
        } else {
            NSString *params = [requestString substringFromIndex:range.location+3];
            [ocNative call:params];
        }
        return NO;
    }  else if ([requestString hasPrefix:@"ios-log:"]) {
        DLog(@"jsconsole: %@", requestString);
        return NO;
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    DLog(@"didFinishLoad");
    [self loadJs];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

- (void)loadJs
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ios" ofType:@"js"];
    NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
    
    filePath = [[NSBundle mainBundle] pathForResource:@"bridge" ofType:@"js"];
    jsString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
    
    filePath = [[NSBundle mainBundle] pathForResource:@"HostApi" ofType:@"js"];
    jsString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
}

- (void)dealloc
{
    [_webView stopLoading];
}
@end
